package com.jiajunsong.blog.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.jiajunsong.blog.domain.Authority;
import com.jiajunsong.blog.domain.Result;
import com.jiajunsong.blog.domain.User;
import com.jiajunsong.blog.repository.AuthorityRepository;
import com.jiajunsong.blog.repository.UserRepository;

@RestController
public class UserController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private AuthorityRepository authorityRepository;


	@GetMapping("/testUserByUsername")
	private Result testUserByUseranme(String username) {
		User user = userRepository.findByUsername(username);
		if (user != null) {
			return new Result("用户存在");
		} else {
			return new Result("");
		}
	}
	
	@GetMapping("/user")
	public Result getUser(Authentication authentication) {
		if(authentication==null) {
			return new Result("未登录");
		}
		
		UserDetails userDetails = (UserDetails)authentication.getPrincipal();
		String userName = userDetails.getUsername();
		
		User user = userRepository.findByUsername(userName);
		return new Result("",user);
	}

	@PostMapping("/user")
	private Result addUser(@RequestBody User user,Authentication authentication) {
		//分两种情况，是新建还是修改
		if(user.getId()!=null) {
			Optional<User> optionalUser = userRepository.findById(user.getId());
			if(optionalUser.isPresent()) {
				User authUser = (User)authentication.getPrincipal();
				User user_q = optionalUser.get();
				if(!user_q.getId().equals(authUser.getId())) {
					return new Result("你只能修改自己的用户信息，不能更改别人的");
				}
				if(!user.getPassword().equals(user_q.getPassword())) {
					user_q.setPassword(passwordEncoder.encode(user.getPassword()));
				}
				user_q.setNickname(user.getNickname());
				user_q.setEmail(user.getEmail());
				try {
					userRepository.save(user_q);
					return new Result();
				} catch (Exception e) {
					return new Result(e.getMessage());
				}
			}
		}
		Authority authority = authorityRepository.getOne(2L);// 设置默认角色ROLE_USER
		List<Authority> authorities = new ArrayList<>();
		authorities.add(authority);
		user.setAuthorities(authorities);
		user.setPassword(passwordEncoder.encode(user.getPassword()));
		try {
			userRepository.save(user);
			return new Result();

		} catch (Exception e) {
			return new Result(e.getMessage());
		}
	}

}
